package com.twu.biblioteca;

import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.Movie;
import com.twu.biblioteca.model.Title;
import com.twu.biblioteca.model.User;

import java.util.ArrayList;
import java.util.List;

public class LibraryController {

    private Integer index;
    private List<Title> titles;
    private List<User> users;
    private User loggedUser;

    public LibraryController() {
        this.titles = new ArrayList<Title>();
        this.users = new ArrayList<User>();
        index = new Integer(0);
    }

    public void addTitle(Title title){
        index++;
        title.setId(index);
        titles.add(title);
    }

    public void checkoutTitle(Title title) throws BusinessException {
        if (!isLogged()){
            throw new BusinessException("User not logged");
        }
        if (title.isAvailable()){
            title.setAvailable(false);
            loggedUser.getTitles().add(title);
        } else {
            throw new BusinessException("That title is not available.");
        }
    }

    public void returnTitle(Title title) throws BusinessException{
        if (!isLogged()){
            throw new BusinessException("User not logged");
        }
        if (!loggedUser.getTitles().contains(title)){
            throw new BusinessException("Invalid return.");
        }
        if (!title.isAvailable()){
            title.setAvailable(true);
            loggedUser.getTitles().remove(title);
        } else {
            throw new BusinessException("That is not a valid title to return.");
        }
    }

    public Title findTitleById(int id) {
        for (Title title : titles) {
            if (title.getId() == id) {
                return title;
            }
        }
        return new Book("", "", 0);
    }

    public List<Movie> getMovies() {
        List<Movie> movies = new ArrayList<Movie>();
        for (Title title: titles){
            if (title instanceof Movie){
                movies.add((Movie) title);
            }
        }
        return movies;
    }

    public List<Book> getBooks(){
        List<Book> books = new ArrayList<Book>();
        for (Title title: titles){
            if (title instanceof Book){
                books.add((Book) title);
            }
        }
        return books;
    }

    public List<User> getUsers(){
        return users;
    }

    public void login(String libraryNumber, String password) throws BusinessException{
        if (isLogged()){
            throw new BusinessException("The system already have a user logged. Do logout first.");
        }
        for (User user : users){
            if (user.getLibraryNumber().equals(libraryNumber) && user.getPassword().equals(password) ){
                loggedUser = user;
            }
        }
        if (loggedUser == null){
            throw new BusinessException("Library Number or password incorrect.");
        }

    }

    public void logout(){
        if (isLogged()){
            loggedUser = null;
        }
    }

    public void addUser(User user) {
        index++;
        user.setId(index);
        users.add(user);
    }

    public boolean isLogged(){
        return loggedUser != null;
    }

    public User getLoggedUser() {
        return loggedUser;
    }

    public User getUserByTitle(Title title){
        for(User user : users){
            if (user.getTitles().contains(title)){
                return user;
            }
        }
        return new User("","","","","");
    }
}
